import requests
from lxml import html
import csv

mhtml = requests.get("http://data.sports.sohu.com/nba/nba_players_rank.php?order_by=points&spm=smpc.fb-nba-home.top-dc.2.1620824904040nf6byr7")
mhtml.encoding='GBK'

hmhtml = html.fromstring(mhtml.text)

headlis = hmhtml.xpath('//div[@class="blue"]/ul/li')

header = []

result = []
for i in range(len(headlis)):
    if i!=5:
        header.append(headlis[i].text)
    else:
        header.append(headlis[i].xpath('div/text()')[0])

innerTrs = hmhtml.xpath('//table[@class="table01"]/tr')

for i in innerTrs:
    resu = {}
    tds = i.xpath('td')
    for j in range(len(tds)):
        if j!=1 and j!=2:
            resu[header[j]] = tds[j].text
        else:
           resu[header[j]] = tds[j].xpath('a/text()')[0]
    result.append(resu)



with open('./haha.csv','a',encoding='utf-8') as f:
    csv_write = csv.writer(f)
    csv_write.writerow(header)
    for i in result:
        csv_write.writerow(i.values())
    f.close()
